package com.qk.management.controller;

import com.qk.common.Result;
import com.qk.common.util.AliyunOSSOperators;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.util.Objects;
import java.util.UUID;


/**
 * @author Just、倾城
 * @created 2025-10-08
 * @description
 *
 */

@Slf4j
@RestController
public class FileHandleController {
    @Autowired
    private AliyunOSSOperators oss;
    @PostMapping("/upload")
    public Result upload(@RequestParam(name = "image") MultipartFile  image) {
      log.info("上传图片:{}", image.getOriginalFilename());
        try {
            // 参数校验
            Objects.requireNonNull(image,"文件不能为空");
            Objects.requireNonNull(image.getOriginalFilename(),"文件名不能为空");
            String url = oss.upload(image.getBytes(), UUID.randomUUID() + image.getOriginalFilename().substring(image.getOriginalFilename().lastIndexOf(".")));
            return Result.success(url);
        } catch (Exception e) {
            log.error("上传图片失败:{}", e.getMessage());
            return Result.error("上传图片失败");
        }
    }
}
